package com.zhenyi.jiahua.index.admin;

import java.util.List;

import com.jfinal.kit.Kv;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.jiahua.common.model.JhCourse;
import com.jiahua.common.model.JhSchool;
import com.zhenyi.jiahua.index.JHController;

import cn.hutool.json.JSONUtil;

/**
 * 课程模块
 * 
 * @author cen
 *
 */
public class CourseController extends JHController {

	@Override
	public void list() {
		Kv kv = Kv.create();
		Long schoolId = getParaToLong("schoolId");
		if (schoolId != null) {
			kv.set("schoolId", schoolId);
		}
		String typeStr = PropKit.get("JH_COURSE_TYPES", "");
		String[] types = typeStr.split(",");
		SqlPara sqlPara = Db.use("jiahua").getSqlPara("JHCouseTemplateList", kv);
		Page<Record> pages = Db.use("jiahua").paginate(getPage(), PAGE_SIZE, sqlPara);
		List<Record> list = pages.getList();
		for (Record record : list) {
			Integer type = record.getInt("type");
			if (type != null) {
				record.set("typeStr", types[type]);
			}
		}
		setRainbow(pages);
		setItems(JSONUtil.toJsonStr(pages.getList()));
		render("/jh/admin/course/index.html");
	}

	@Override
	public void submit() {
		Long courseId = getParaToLong("courseId");
		// JhStaff staff = getStaff();
		// Long schoolId = staff.getSchoolId();
		JhCourse course;
		if (courseId == null) {
			course = new JhCourse();
			// course.setSchoolId(schoolId);
			course.save();
		} else {
			course = JhCourse.dao.findById(courseId);
		}
		String name = getPara("name");
		String topics = getPara("topics");
		// topics = StringEscapeUtils.escapeHtml(topics);
		Integer type = getParaToInt("type");
		String tags = getPara("tags");
		Long schoolId = getParaToLong("schoolId");
		Integer isFixed = getParaToInt("isFixed", 0);
		Integer isCount = getParaToInt("isCount", 0);
		Integer courseTime = getParaToInt("courseTime", 60);
		Integer limit = getParaToInt("limit", 0);
		course.setName(name);
		course.setTopics(topics);
		course.setType(type);
		course.setTags(tags);
		course.setSchoolId(schoolId);
		course.setIsFixed(isFixed);
		course.setIsCount(isCount);
		course.setCourseTime(courseTime);
		course.setLimit(limit);
		course.update();
		renderSuccess();
	}

	@Override
	public void info() {
		List<JhSchool> schools = JhSchool.dao.findAll();
		String tags = PropKit.get("JH_COURSE_TAGS");
		String types = PropKit.get("JH_COURSE_TYPES");
		setAttr("schools", JSONUtil.toJsonStr(schools));
		setAttr("tags", tags);
		setAttr("types", types);
		Long courseId = getParaToLong("courseId");
		JhCourse course;
		if (courseId == null) {
			course = new JhCourse();
		} else {
			course = JhCourse.dao.findById(courseId);
		}
		// String topics = course.getTopics();
		setAttr("info", course);
		// setAttr("topics", new JSONObject(topics));
		render("/jh/admin/course/info.html");
	}

	@Override
	public void delete() {
		Long courseId = getParaToLong("courseId");
		JhCourse course = JhCourse.dao.findById(courseId);
		course.setFlag(1);
		course.update();
		renderSuccess();
	}

}
